| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- import type { GetServerSidePropsContext, GetServerSidePropsResult } from 'next';
- import {
- getServerSideI18nProps, getServerSideUserUISettingsProps, getServerSideCommonInitialProps, getServerSideCommonEachProps, isValidCommonEachRouteProps,
- } from '../common-props';
- import type { InitialProps } from '../general-page';
- import {
- getServerSideConfigurationProps, getServerSideRendererConfigProps, getServerSideSidebarConfigProps,
- getActivityAction, isValidInitialAndSameRouteProps,
- } from '../general-page';
- import { addActivity } from '../utils/activity';
- import { mergeGetServerSidePropsResults } from '../utils/server-side-props';
- import { NEXT_JS_ROUTING_PAGE } from './consts';
- import { getPageDataForInitial, getPageDataForSameRoute } from './page-data-props';
- import type { EachProps } from './types';
- const nextjsRoutingProps = {
- props: {
- nextjsRoutingPage: NEXT_JS_ROUTING_PAGE,
- },
- };
- export async function getServerSidePropsForInitial(context: GetServerSidePropsContext): Promise<GetServerSidePropsResult<InitialProps & EachProps>> {
- //
- // STAGE 1
- //
- const commonEachPropsResult = await getServerSideCommonEachProps(context);
- // Handle early return cases (redirect/notFound)
- if ('redirect' in commonEachPropsResult || 'notFound' in commonEachPropsResult) {
- return commonEachPropsResult;
- }
- const commonEachProps = await commonEachPropsResult.props;
- // Handle redirect destination from common props
- if (commonEachProps.redirectDestination != null) {
- return {
- redirect: {
- permanent: false,
- destination: commonEachProps.redirectDestination,
- },
- };
- }
- //
- // STAGE 2
- //
- const [
- commonInitialResult,
- userUIResult,
- serverConfigResult,
- rendererConfigResult,
- sidebarConfigResult,
- i18nPropsResult,
- pageDataResult,
- ] = await Promise.all([
- getServerSideCommonInitialProps(context),
- getServerSideUserUISettingsProps(context),
- getServerSideConfigurationProps(context),
- getServerSideRendererConfigProps(context),
- getServerSideSidebarConfigProps(context),
- getServerSideI18nProps(context, ['translation']),
- getPageDataForInitial(context),
- ]);
- // Merge all results in a type-safe manner (using sequential merging)
- const mergedResult = mergeGetServerSidePropsResults(commonEachPropsResult,
- mergeGetServerSidePropsResults(commonInitialResult,
- mergeGetServerSidePropsResults(userUIResult,
- mergeGetServerSidePropsResults(serverConfigResult,
- mergeGetServerSidePropsResults(rendererConfigResult,
- mergeGetServerSidePropsResults(sidebarConfigResult,
- mergeGetServerSidePropsResults(i18nPropsResult,
- mergeGetServerSidePropsResults(pageDataResult, nextjsRoutingProps))))))));
- // Check for early return (redirect/notFound)
- if ('redirect' in mergedResult || 'notFound' in mergedResult) {
- return mergedResult;
- }
- const mergedProps = await mergedResult.props;
- // Type-safe props validation AFTER skipSSR is properly set
- if (!isValidInitialAndSameRouteProps(mergedProps)) {
- throw new Error('Invalid merged props structure');
- }
- await addActivity(context, getActivityAction(mergedProps));
- return mergedResult;
- }
- export async function getServerSidePropsForSameRoute(context: GetServerSidePropsContext): Promise<GetServerSidePropsResult<EachProps>> {
- //
- // STAGE 1
- //
- const commonEachPropsResult = await getServerSideCommonEachProps(context);
- // Handle early return cases (redirect/notFound)
- if ('redirect' in commonEachPropsResult || 'notFound' in commonEachPropsResult) {
- return commonEachPropsResult;
- }
- const commonEachProps = await commonEachPropsResult.props;
- // Handle redirect destination from common props
- if (commonEachProps.redirectDestination != null) {
- return {
- redirect: {
- permanent: false,
- destination: commonEachProps.redirectDestination,
- },
- };
- }
- //
- // STAGE 2
- //
- // Get page data
- const sameRoutePageDataResult = await getPageDataForSameRoute(context);
- // Merge results in a type-safe manner
- const mergedResult = mergeGetServerSidePropsResults(commonEachPropsResult,
- mergeGetServerSidePropsResults(sameRoutePageDataResult, nextjsRoutingProps));
- // Check for early return (redirect/notFound)
- if ('redirect' in mergedResult || 'notFound' in mergedResult) {
- return mergedResult;
- }
- // Validate the merged props have all required properties
- if (!isValidCommonEachRouteProps(mergedResult.props)) {
- throw new Error('Invalid same route props structure');
- }
- // -- TODO: persist activity
- // const mergedProps = await mergedResult.props;
- // // Type-safe props validation AFTER skipSSR is properly set
- // if (!isValidSameRouteProps(mergedProps)) {
- // throw new Error('Invalid same route props structure');
- // }
- // await addActivity(context, getActivityAction(mergedProps));
- return mergedResult;
- }
|